<?php

use App\Common\DbSelector;
use App\Process\MonitorProcess;
use Swoft\Db\Pool;
use Swoft\Http\Server\HttpServer;
use Swoft\Task\Swoole\SyncTaskListener;
use Swoft\Task\Swoole\TaskListener;
use Swoft\Task\Swoole\FinishListener;
use Swoft\Rpc\Client\Client as ServiceClient;
use Swoft\Rpc\Client\Pool as ServicePool;
use Swoft\Rpc\Server\ServiceServer;
use Swoft\Http\Server\Swoole\RequestListener;
use Swoft\WebSocket\Server\WebSocketServer;
use Swoft\Server\SwooleEvent;
use Swoft\Db\Database;
use Swoft\Redis\RedisDb;
use Swoft\Log\Handler\FileHandler;
use App\Common\RpcProvider;


return [
    'config'   => [
        'path' => __DIR__ . '/../config',
        'env' => 'dev'
    ],
    'consul' => [
        'host' => env('CONSUL_HOST','192.168.99.100'),
    ],

    // --------------------日志--------------------
    'logger'           => [
        'flushRequest' => false,
        'enable'       => true,
        'handlers'     => [
            'application' => bean('applicationHandler'),
            'notice'      => bean('noticeHandler'),
        ],
    ],
    'lineFormatter'      => [
        'format'     => '%datetime% [%level_name%] [%channel%] [%event%] [tid:%tid%] [cid:%cid%] [traceid:%traceid%] [spanid:%spanid%] [parentid:%parentid%] %messages%',
        'dateFormat' => 'Y-m-d H:i:s',
    ],
    'noticeHandler'      => [
        'class'     => FileHandler::class,
        'logFile'   => '@runtime/logs/notice.log',
        'formatter' => bean('lineFormatter'),
        'levels'    => env('LOG_NOTICE_LEVERS', 'notice,info,debug,trace'),
    ],
    'applicationHandler' => [
        'class'     => FileHandler::class,
        'logFile'   => '@runtime/logs/error.log',
        'formatter' => bean('lineFormatter'),
        'levels'    => env('LOG_ERROR_LEVERS', 'error,warning'),
    ],


    // ------------------------server-------------------------------
    'httpServer'       => [
        'class'    => HttpServer::class,
        'port'    => env("HTTP_PORT", 18306),
        'listener' => [
            'rpc' => bean('rpcServer')
        ],
        'process' => [
//            'monitor' => bean(MonitorProcess::class)
        ],
        'on'       => [
//            SwooleEvent::TASK   => bean(SyncTaskListener::class),  // Enable sync task
            SwooleEvent::TASK   => bean(TaskListener::class),  // Enable task must task and finish event
            SwooleEvent::FINISH => bean(FinishListener::class)
        ],
        /* @see HttpServer::$setting */
        'setting'  => [
            'task_worker_num'       => 12,
            'task_enable_coroutine' => true
        ]
    ],
    'httpDispatcher'   => [
        // Add global http middleware
        'middlewares' => [
            // Allow use @View tag
            \Swoft\View\Middleware\ViewMiddleware::class,
            \App\Http\Middleware\ControllerMiddleware::class,

        ],
    ],
    'db'  => [
        'class'  => Database::class,
        'charset'  => 'utf8mb4',
        'prefix'   => 'happy_',
        'options'  => [],
        'config'   => [
            'collation' => 'utf8mb4_general_ci',
            'strict'    => false,
            'timezone'  => '+8:00',
            'modes'     => 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES',
        ],
        'writes' => [
            [
                'dsn'      => 'mysql:dbname=swoft_happy1;host=192.168.99.1;port=3306',
                'username' => 'root',
                'password' => 'root',
            ],
        ],
        'reads'  => [
            [
                'dsn'      => 'mysql:dbname=swoft_happy2;host=192.168.99.1;port=3306',
                'username' => 'root',
                'password' => 'root',
            ],
        ],
    ],
    'db.pool'         => [
        'class'    => Pool::class,
        'database' => bean('db'),
        'minActive'   => 10,
        'maxActive'   => 20,
        'maxWait'     => 0,
        'maxWaitTime' => 0,
        'maxIdleTime' => 60,

    ],

    'db2'         => [
        'class'     => Database::class,
        'dsn'       => 'mysql:dbname=demo;host=192.168.99.1;port=3306',
        'username'  => 'root',
        'password'  => 'root',
        'charset'   => 'utf8mb4',
    ],
    'db2.pool'         => [
        'class'    => Pool::class,
        'database' => bean('db2'),
        'minActive'   => 10,
        'maxActive'   => 20,
        'maxWait'     => 0,
        'maxWaitTime' => 0,
        'maxIdleTime' => 60,
    ],

    'migrationManager' => [
        'migrationPath' => '@app/Migration',
    ],
    'redis'            => [
        'class'    => RedisDb::class,
        'host'     => '192.168.99.1',
        'port'     => 6379,
        'password' => '',
        'database' => 1,
        'retryInterval' => 10,
        'readTimeout'   => 0,
        'timeout'       => 2,
        'option' => [
            'prefix' => 'swoft:',
            'serializer' => Redis::SERIALIZER_PHP
        ]
    ],
    'redis.pool'     => [
        'class'   => \Swoft\Redis\Pool::class,
        'redisDb' => \bean('redis'),
        'minActive'   => 10,
        'maxActive'   => 20,
        'maxWait'     => 0,
        'maxWaitTime' => 0,
        'maxIdleTime' => 60,
    ],

    'user'             => [
        'class'   => ServiceClient::class,
        'host'    => '127.0.0.1',
        'port'    => 18887,
        'setting' => [
            'timeout'         => 0.5,
            'connect_timeout' => 1.0,
            'write_timeout'   => 10.0,
            'read_timeout'    => 0.5,
        ],
        'packet'  => bean('rpcClientPacket'),
        'provider' => bean(RpcProvider::class)
    ],
    'user.pool'        => [
        'class'  => ServicePool::class,
        'client' => bean('user')
    ],


    'rpcServer'        => [
        'class' => ServiceServer::class,
        'host'    => '0.0.0.0',
        'port'    => env("RPC_PORT", 18307)
    ],
    'wsServer'         => [
        'class'   => WebSocketServer::class,
        'port'    => 18308,
        'on'      => [
            // Enable http handle
            SwooleEvent::REQUEST => bean(RequestListener::class),
        ],
        'debug'   => env('SWOFT_DEBUG', 0),
        /* @see WebSocketServer::$setting */
        'setting' => [
            'log_file' => alias('@runtime/swoole.log'),
        ],
    ],
    'tcpServer'         => [
        'port'  => 18309,
        'debug' => 1,
    ],
    /** @see \Swoft\Tcp\Protocol */
    'tcpServerProtocol' => [
        'type'            => \Swoft\Tcp\Packer\SimpleTokenPacker::TYPE,
        // 'openLengthCheck' => true,
    ],
    'cliRouter'         => [
        // 'disabledGroups' => ['demo', 'test'],
    ],


];
